





function register() {
    let username = document.querySelector('input[name="username"]').value
    let password = document.querySelector('input[name="password"]').value
    myAjaxPromise({
        url: 'http://api.yuguoxy.com/api/sign',
        method: 'POST',
        data: {
            username,
            password,
            headerimg: 'www.baidu.com'
        },
    }).then(res => {
        console.log(res);
        if(res.resultCode == 1){
            localStorage.setItem('USER', JSON.stringify(username))
            location.href = './index.html'
        }
        
    }).catch(err => {
        console.log(err);
    })

}

function getRegister() {

    let btn = document.querySelector('form')

    btn.addEventListener('submit', function (e) {
        e = e || window.event
        e.preventDefault()
        let checkEle = document.querySelector('input[name="agreement"]')
        if (checkEle.checked) {
            
            let u = userNull()
            let p = passwordNull()

            if (u && p) {
                register()
                
                
            }
        }else{
            alert('请勾选已阅读并同意以下协议！')
        }

    })
}
// 验证账号非空
function userNull() {
    let userP = document.querySelector('.user-p')
    let username = document.querySelector('input[name="username"]')

    if (username.value == '') {
        userP.innerHTML = '账号不能为空！'
        return false
    } else {
        userP.innerHTML = ''
        return true
    }

}
// 验证密码非空
function passwordNull() {
    let passwordP = document.querySelector('.password-p')
    let password = document.querySelector('input[name="password"]')
    if (password.value == '') {
        passwordP.innerHTML = '密码不能为空！'
        return false
    } else {
        passwordP.innerHTML = ''
        return true
    }
}
// 失去焦点
function loseFocus() {
    let username = document.querySelector('input[name="username"]')
    let password = document.querySelector('input[name="password"]')
    username.onblur = function () {
        userNull()
    }
    password.onblur = function () {
        passwordNull()
    }
}
// 跳转到登录界面
function goLogin() {
    let loginEle = document.querySelector('.go-login')
    loginEle.onclick = function () {
        location.href = './login.html'
    }
}



getRegister()
loseFocus()
goLogin()